using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace OpenComposite.EII.Designer.Workflow.Forms
{
	public partial class FormMashupControlGroups : Form
	{
		#region Initialization
		public FormMashupControlGroups(Repository.Workflow workflow)
		{
			if ( workflow == null ) throw new ArgumentNullException("workflow");

			_workflow = workflow;

			InitializeComponent();

			btnAddGroup.Enabled = false;
			btnRemoveGroup.Enabled = false;
		}
		#endregion

		#region Private Members

		#region Fields
		private Repository.Workflow _workflow;
		private Point _ptDragStart = Point.Empty;
		#endregion

		#region Methods
		private void displayControls()
		{
			dgvControls.DataSource = new BindingSource(_workflow.ExtendedProperties.MashupControls.Values, "");
		}
		private void displayGroups()
		{
			lboGroups.DataSource = new BindingSource(_workflow.ExtendedProperties.MashupGroups, "");
		}
		#endregion

		#region Event Handlers
		private void FormMashupControlGroups_Load(object sender, EventArgs e)
		{
			dgvControls.AutoGenerateColumns = false;
			displayControls();
			displayGroups();
		}

		private void txtGroup_TextChanged(object sender, EventArgs e)
		{
			btnAddGroup.Enabled = ( txtGroup.TextLength > 0 );
		}

		private void btnAddGroup_Click(object sender, EventArgs e)
		{
			Repository.Workflow.MashupGroup group = new OpenComposite.EII.Repository.Workflow.MashupGroup();
			group.Name = txtGroup.Text;
			_workflow.ExtendedProperties.MashupGroups.Add(group);
			txtGroup.Clear();
			displayGroups();
		}

		private void btnRemoveGroup_Click(object sender, EventArgs e)
		{
			Repository.Workflow.MashupGroup group = lboGroups.SelectedItem as Repository.Workflow.MashupGroup;
			if ( group != null ) {
				_workflow.ExtendedProperties.MashupGroups.Remove(group);
				foreach ( Repository.Workflow.MashupControl ctrl in _workflow.ExtendedProperties.MashupControls.Values ) {
					if ( ctrl.GroupName == group.Name ) {
						ctrl.GroupName = "";
					}
				}
			}
			displayGroups();
			displayControls();
		}

		private void lboGroups_SelectedIndexChanged(object sender, EventArgs e)
		{
			btnRemoveGroup.Enabled = ( lboGroups.SelectedIndices.Count > 0 );
		}

		private void dgvControls_DragOver(object sender, DragEventArgs e)
		{
			if ( !e.Data.GetDataPresent(typeof(Repository.Workflow.MashupGroup)) ) return;

			Point ptClient = dgvControls.PointToClient(new Point(e.X, e.Y));
			DataGridView.HitTestInfo hti = dgvControls.HitTest(ptClient.X, ptClient.Y);
			if ( hti.Type == DataGridViewHitTestType.Cell &&
				 hti.RowIndex >= 0 && hti.RowIndex < _workflow.ExtendedProperties.MashupControls.Count ) {
				e.Effect = DragDropEffects.Link;
			} else {
				e.Effect = DragDropEffects.None;
			}
		}

		private void dgvControls_DragDrop(object sender, DragEventArgs e)
		{
			if ( !e.Data.GetDataPresent(typeof(Repository.Workflow.MashupGroup)) ) return;

			Point ptClient = dgvControls.PointToClient(new Point(e.X, e.Y));
			DataGridView.HitTestInfo hti = dgvControls.HitTest(ptClient.X, ptClient.Y);
			if ( hti.Type == DataGridViewHitTestType.Cell &&
				 hti.RowIndex >= 0 && hti.RowIndex < _workflow.ExtendedProperties.MashupControls.Count ) {
				Repository.Workflow.MashupGroup group = (Repository.Workflow.MashupGroup)e.Data.GetData(typeof(Repository.Workflow.MashupGroup));
				( (Repository.Workflow.MashupControl)dgvControls.Rows[hti.RowIndex].DataBoundItem )
					.GroupName = group.Name;
				dgvControls.UpdateCellValue(colGroup.Index, hti.RowIndex);
			}
		}

		private void lboGroups_MouseDown(object sender, MouseEventArgs e)
		{
			_ptDragStart = e.Location;
		}

		private void lboGroups_MouseUp(object sender, MouseEventArgs e)
		{
			_ptDragStart = Point.Empty;
		}

		private void lboGroups_MouseMove(object sender, MouseEventArgs e)
		{
			if ( _ptDragStart == Point.Empty || e.Button != MouseButtons.Left ) return;

			if ( lboGroups.SelectedItem != null &&
				 ( Math.Abs(_ptDragStart.X - e.X) > 5 || Math.Abs(_ptDragStart.Y - e.Y) > 5 ) ) {
				lboGroups.DoDragDrop(lboGroups.SelectedItem, DragDropEffects.Link);
				_ptDragStart = Point.Empty;
			}
		}

		#endregion

		#endregion

	}
}